package com.qunar.train.mock.util;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import java.io.File;
import java.io.IOException;

public class QunarFileUtil {
    public static String getQunarStore() {
        return store.get();
    }

    private static Supplier<String> store = Suppliers.memoize(new Supplier<String>() {
        @Override
        public String get() {
            String path = System.getProperty("qunar.cache", null);

            if (path == null) {
                path = System.getProperty("catalina.base");
                if (path == null) path = System.getProperty("java.io.tmpdir");
                path = path + File.separator + "cache";
                System.setProperty("qunar.cache", path);
            }

            File file = new File(path);
            file.mkdirs();

            try {
                path = file.getCanonicalPath();
            } catch (IOException e) {
                AegisLog.error(e);
            }

            return path;
        }
    });
}
